package com.sugon.utils;

import cn.hutool.core.util.StrUtil;
import com.sugon.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * Shiro工具类
 *
 * @author YuXD
 * @date 2022-05-25
 */
public class ShiroUtils {

    /**
     * getSession
     *
     * @return
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * getSubject
     *
     * @return
     */
    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    /**
     * getUserEntity
     *
     * @return
     */
    public static SysUserEntity getUserEntity() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            return (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        }
        return null;
    }

    /**
     * 获取当前登录用户ID
     *
     * @return
     */
    public static String getUserId() {
        SysUserEntity userEntity = getUserEntity();
        if (userEntity != null) {
            return userEntity.getUserId();
        }
        return null;
    }

    public static String getUnionId() {
        SysUserEntity userEntity = getUserEntity();
        if (userEntity != null) {
            return userEntity.getUnionId();
        }
        return null;
    }

    /**
     * 获取用户姓名
     *
     * @return
     */
    public static String getUserRealName() {
        SysUserEntity userEntity = getUserEntity();
        if (userEntity != null) {
            return userEntity.getNickName();
        }
        return null;
    }

    /**
     * getUserName
     *
     * @return
     */
    public static String getUserName() {
        SysUserEntity userEntity = getUserEntity();
        if (userEntity != null) {
            return userEntity.getUserName();
        }
        return null;
    }

    /**
     * getDeptId
     *
     * @return
     */
    public static String getDeptId() {
        SysUserEntity userEntity = getUserEntity();
        if (userEntity != null) {
            return userEntity.getDeptId();
        }
        return null;
    }

    /**
     * 获取用户所在车间ID
     *
     * @return
     */
    public static String getWorkshop() {
        String deptId = getDeptId();
        if (deptId != null) {
            return StrUtil.sub(deptId, 0, 4);
        }
        return null;
    }

    /**
     * 获取所在车间
     *
     * @return
     */
    public static String getDeptSubCode() {
        SysUserEntity userEntity = getUserEntity();
        if (userEntity != null) {
            return userEntity.getDeptCode();
        }
        return null;
    }

    /**
     * setSessionAttribute
     *
     * @param key
     * @param value
     */
    public static void setSessionAttribute(Object key, Object value) {
        getSession().setAttribute(key, value);
    }

    /**
     * getSessionAttribute
     *
     * @param key
     * @return
     */
    public static Object getSessionAttribute(Object key) {
        return getSession().getAttribute(key);
    }

    /**
     * isLogin
     *
     * @return
     */
    public static boolean isLogin() {
        return SecurityUtils.getSubject().getPrincipal() != null;
    }

    /**
     * logout
     */
    public static void logout() {
        SecurityUtils.getSubject().logout();
    }

    /**
     * getKaptcha
     *
     * @param key
     * @return
     */
    public static String getKaptcha(String key) {
        String kaptcha;
        try {
            kaptcha = getSessionAttribute(key).toString();
            getSession().removeAttribute(key);
        } catch (Exception e) {
            return null;
        }
        return kaptcha;
    }
}
